package meiyunting.shiwuzhaoling.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务发现控制器
 * 用于检查服务注册情况
 */
@RestController
@RequestMapping("/api/discovery")
public class ServiceDiscoveryController {

    @Autowired
    private DiscoveryClient discoveryClient;
    
    /**
     * 获取所有注册的服务
     */
    @GetMapping("/services")
    public Map<String, Object> getServices() {
        Map<String, Object> result = new HashMap<>();
        List<String> services = discoveryClient.getServices();
        
        result.put("services", services);
        result.put("count", services.size());
        
        System.out.println("【服务发现】发现服务数量: " + services.size());
        services.forEach(service -> System.out.println("【服务发现】服务名称: " + service));
        
        return result;
    }
    
    /**
     * 获取指定服务的所有实例
     */
    @GetMapping("/instances")
    public Map<String, Object> getInstances() {
        Map<String, Object> result = new HashMap<>();
        String serviceId = "lost-found-service";
        
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        
        List<Map<String, Object>> instanceDetails = instances.stream()
                .map(instance -> {
                    Map<String, Object> details = new HashMap<>();
                    details.put("instanceId", instance.getInstanceId());
                    details.put("host", instance.getHost());
                    details.put("port", instance.getPort());
                    details.put("uri", instance.getUri().toString());
                    details.put("metadata", instance.getMetadata());
                    return details;
                })
                .collect(Collectors.toList());
        
        result.put("serviceId", serviceId);
        result.put("instances", instanceDetails);
        result.put("count", instances.size());
        
        System.out.println("【服务发现】服务 " + serviceId + " 的实例数量: " + instances.size());
        instances.forEach(instance -> {
            System.out.println("【服务发现】实例ID: " + instance.getInstanceId() + 
                    ", 主机: " + instance.getHost() + 
                    ", 端口: " + instance.getPort() + 
                    ", 权重: " + instance.getMetadata().getOrDefault("weight", "未设置"));
        });
        
        return result;
    }

    /**
     * 服务健康检查端点
     * @return 健康状态
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("service", "lost-found-service");
        health.put("timestamp", System.currentTimeMillis());
        return health;
    }
} 